// pages/cart/addresslist/increaseaddress/increaseaddress.js
import { validate } from '../../../../utils/validate.js'
import { axios } from '../../../../utils/ajax.js'
import { region } from '../../../../data/region.js' 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userid:'',
    token:'',
    //配送时间
    delievertime:['周一到周五(工作日)','周六周日(周末)','任何时间都行'],
    //省
    provinceId: '',
    //市
    cityId: '',
    //区域
    districtId: '',
    //省市区是否已经选择
    hasaddress: false,
    //待编辑的数据
    editdata:'',
    consignee:'',
    tel: '',
    //配送时间 都先不显示
    deliveryTime:'0',
    address:'',
    isDefault:'',
    //传给地址组件的序列号集合
    multiIndex: [0, 0, 0],
    addressid:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let { userid = '', token = '' } = wx.getStorageSync('userinfo') 
    this.setData({
      userid: userid,
      token:token
    })
    //如果从地址编辑进来
    if (options.addressid){
      wx.setNavigationBarTitle({
        title: '编辑地址',
      })
      this.setData({
        hasaddress: true,
        addressid: options.addressid
      })
      let pages = getCurrentPages();
      let prevPage = pages[pages.length - 2];  // 获取上一页实例   可以获取上一页的数据并改变
      let prevdata = prevPage.data.data        //获取上一页地址数据
      let editdata = prevdata.find(x => x.addressId == options.addressid)   //查询待编辑的数据

      let provinceId = prevdata.find(x => x.addressId == this.data.addressid).provinceId
      let cityId = prevdata.find(x => x.addressId == this.data.addressid).cityId
      let districtId = prevdata.find(x => x.addressId == this.data.addressid).districtId
      let isDefault = prevdata.find(x => x.addressId == this.data.addressid).isDefault

      console.log(provinceId, cityId, districtId)
      this.setData({
        editdata: editdata,
        provinceId: provinceId,
        cityId: cityId,
        districtId: districtId,
        isDefault: isDefault
      })
      this.setData({
        consignee: editdata.consignee,
        tel: editdata.tel,
        // deliveryTime: editdata.deliveryTime,
        address: editdata.address
      })
      let multiIndex = [0,0,0]
      multiIndex[0] = region.findIndex(x => x.id == editdata.provinceId)
      multiIndex[1] = region.find(x => x.id == editdata.provinceId).childs.findIndex(x => x.id == editdata.cityId)
      multiIndex[2] = region.find(x => x.id == editdata.provinceId).childs.find(x => x.id == editdata.cityId).childs.findIndex(x => x.id == editdata.districtId)
      this.setData({
        multiIndex: multiIndex
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  // //选择配送时间
  // choosedelievetime (e) {
  //   this.setData({
  //     deliveryTime: this.data.delievertime[e.detail.value]
  //   })
  // },
  //点击保存
  savemsg (e) {
    let obj = {
      consignee: e.detail.value.username,
      tel: e.detail.value.tel,
      // deliveryTime: this.data.deliveryTime,
      address: e.detail.value.detailaddress
    }
    //编辑地址
    if (this.data.addressid){
      if (validate(obj)) {
        console.log(this.data.provinceId)
        if (this.data.provinceId) {
          //编辑地址
          axios({
            url: '/address/modify',
            data: {
              userid: this.data.userid,
              token: this.data.token,
              addressId: this.data.addressid,
              consignee: obj.consignee,
              tel: obj.tel,
              provinceId: this.data.provinceId,
              cityId: this.data.cityId,
              districtId: this.data.districtId,
              address: obj.address,
              deliveryTime: obj.deliveryTime
            }
          }).then((res) => {
            console.log(res)
            //省市区依靠id查名字
            let provinceName = region.find(x => x.id == this.data.provinceId).name
            let cityName = region.find(x => x.id == this.data.provinceId).childs.find(x => x.id == this.data.cityId).name
            let districtName = region.find(x => x.id == this.data.provinceId).childs.find(x => x.id == this.data.cityId).childs.find(x => x.id == this.data.districtId).name

            let pages = getCurrentPages();
            let prevPage = pages[pages.length - 2];  // 获取上一页实例   可以获取上一页的数据并改变
            let info = prevPage.data.data            //获取上一页的地址数据进行操作
            info.find(x => x.addressId == this.data.addressid).address = obj.address
            info.find(x => x.addressId == this.data.addressid).addressId = this.data.addressid
            info.find(x => x.addressId == this.data.addressid).cityId = this.data.cityId
            info.find(x => x.addressId == this.data.addressid).consignee = obj.consignee
            // info.find(x => x.addressId == this.data.addressid).deliveryTime = obj.deliveryTime
            info.find(x => x.addressId == this.data.addressid).districtId = this.data.districtId
            info.find(x => x.addressId == this.data.addressid).tel = obj.tel
            info.find(x => x.addressId == this.data.addressid).provinceName = provinceName
            info.find(x => x.addressId == this.data.addressid).cityName = cityName
            info.find(x => x.addressId == this.data.addressid).districtName = districtName
            prevPage.setData({                        //设置上一页的数据
              data: info
            })
            wx.showToast({
              title: '保存成功',
              icon: 'none'
            })
            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              })
            }, 2000)
          })
        }else{
          wx.showToast({
            title: '请选择所在地区',
          })
        }
      }
    //新增地址
    }else{
      if (validate(obj)) {
        console.log(this.data.provinceId)
        if (this.data.provinceId){
          //新增地址  新增的就是默认地址
          axios({
            url: '/address/add',
            data: {
              userid: this.data.userid,
              token: this.data.token,
              consignee: obj.consignee,
              tel: obj.tel,
              provinceId: this.data.provinceId,
              cityId: this.data.cityId,
              districtId: this.data.districtId,
              address: obj.address,
              deliveryTime: obj.deliveryTime
            }
          }).then((res) => {
            console.log(res)
            //省市区依靠id查名字
            let provinceName = region.find(x => x.id == this.data.provinceId).name
            let cityName = region.find(x => x.id == this.data.provinceId).childs.find(x => x.id == this.data.cityId).name
            let districtName = region.find(x => x.id == this.data.provinceId).childs.find(x => x.id == this.data.cityId).childs.find(x => x.id == this.data.districtId).name

            let pages = getCurrentPages();
            let prevPage = pages[pages.length - 2];  // 获取上一页实例   可以获取上一页的数据并改变
            let info = prevPage.data.data            //获取上一页的地址数据进行操作
            console.log(prevPage.route)
            console.log(this.data)
            //从地址列表进来
            if (prevPage.route == 'pages/cart/addresslist/addresslist'){
              if (info) {
                info.map(x => x.isDefault = 0)
              }
              let newmessage = {
                address: obj.address,
                addressId: res.data.data.address_id,
                cityId: this.data.cityId,
                consignee: obj.consignee,
                // deliveryTime: obj.deliveryTime,
                districtId: this.data.districtId,
                provinceId: this.data.provinceId,
                tel: obj.tel,
                isDefault: 1,
                provinceName: provinceName,
                cityName: cityName,
                districtName: districtName
              }
              info.push(newmessage)
              prevPage.setData({                        //设置上一页的数据
                data: info
              })
              wx.showToast({
                title: '保存成功',
                icon: 'none'
              })
              setTimeout(function () {
                wx.navigateBack({
                  delta: 1
                })
              }, 2000)
            //从购物车没地址的时候进来
            } else if (prevPage.route == 'pages/cart/cart'){
              setTimeout(function () {
                wx.navigateBack({
                  delta: 1
                })
              }, 2000)
            }           
          })
        }else{
          wx.showToast({
            title: '请选择所在地区',
            icon:"none"
          })
        }
      }
    }
  },
  //获取选中的地址id
  getaddressid (e){
    console.log(e.detail)
    this.setData({
      //省
      provinceId: e.detail[0],
      //市
      cityId: e.detail[1],
      //区域
      districtId: e.detail[2]
    })
  }
})